﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

namespace DAO
{
    public class KhuyenMaiDAO : AbstractDAO<DTO.KhuyenMaiDTO>
    {
        protected override string GetTableName()
        {
            return "khuyenmai";
        }

        public override DTO.KhuyenMaiDTO GetByDataRow(System.Data.DataRow row)
        {
            DTO.KhuyenMaiDTO khuyenmai = new DTO.KhuyenMaiDTO();
            int id, phantram;
            DateTime batdau, ketthuc;
            int.TryParse(row["chuyenxe_id"].ToString(), out id);
            int.TryParse(row["phantram"].ToString(), out phantram);
            DateTime.TryParse(row["thoigianbatdau"].ToString(), out batdau);
            DateTime.TryParse(row["thoigianketthuc"].ToString(), out ketthuc);

            khuyenmai.Id = id;
            khuyenmai.Phantram = phantram;
            khuyenmai.Batdau = batdau;
            khuyenmai.Ketthuc = ketthuc;
            
            return khuyenmai;
        }

        public DTO.KhuyenMaiDTO getById(String chuyenxe_id)
        {
            DTO.KhuyenMaiDTO khuyenmai = new DTO.KhuyenMaiDTO();
            IList<DTO.KhuyenMaiDTO> ls = GetAllWhere("chuyenxe_id = " + chuyenxe_id);
            khuyenmai = ls[0];
            return khuyenmai;
        }

        public bool Edit(DTO.KhuyenMaiDTO khuyenmaiDTO, DTO.KhuyenMaiDTO newkhuyenmaiDTO)
        {
            IList<SqlParameter> listParameter = new List<SqlParameter>();
            listParameter.Add(new SqlParameter("@ChuyenXeId", khuyenmaiDTO.Id));
            listParameter.Add(new SqlParameter("@PhanTram", khuyenmaiDTO.Phantram));
            listParameter.Add(new SqlParameter("@BatDau", khuyenmaiDTO.Batdau));
            listParameter.Add(new SqlParameter("@KetThuc", khuyenmaiDTO.Ketthuc));
            listParameter.Add(new SqlParameter("@ChuyenXeIdMoi", newkhuyenmaiDTO.Id));
            listParameter.Add(new SqlParameter("@PhanTramMoi", newkhuyenmaiDTO.Phantram));
            listParameter.Add(new SqlParameter("@BatDauMoi", newkhuyenmaiDTO.Batdau));
            listParameter.Add(new SqlParameter("@KetThucMoi", newkhuyenmaiDTO.Ketthuc));
            SqlParameter thongBao = new SqlParameter("@ThanhCong", System.Data.SqlDbType.Bit);
            thongBao.Direction = System.Data.ParameterDirection.Output;
            listParameter.Add(thongBao);

            this.dataProvider.ExecuteStoreProc(
                "Proc_CapNhatKhuyenMai",
                listParameter);
            return (bool)thongBao.Value;
        }

        public bool AddNew(DTO.KhuyenMaiDTO khuyenmaiDTO)
        {
            IList<SqlParameter> listParameter = new List<SqlParameter>();
            listParameter.Add(new SqlParameter("@ChuyenXeId", khuyenmaiDTO.Id));
            listParameter.Add(new SqlParameter("@PhanTram", khuyenmaiDTO.Phantram));
            listParameter.Add(new SqlParameter("@ThoiGianBatDau", khuyenmaiDTO.Batdau));
            listParameter.Add(new SqlParameter("@ThoiGianKetThuc", khuyenmaiDTO.Ketthuc));
            SqlParameter sqlThongBao = new SqlParameter("@ThanhCong", SqlDbType.Bit);
            sqlThongBao.Direction = System.Data.ParameterDirection.Output;
            listParameter.Add(sqlThongBao);

            this.dataProvider.ExecuteStoreProc(
                "Proc_ThemKhuyenMai",
                listParameter);
            return (bool)sqlThongBao.Value;
        }
    }
}
